<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Car extends Access_Controller {

	function __construct()
	{
		parent::__construct();
		$this->load->model('crud_model');
		$this->load->model('car_model');
	}
	//
	////	THIS SECTION FOR OVERVIEW
	function index()
	{
		$page["_contents"] = "administrator/car/v_car";
		$data['type'] = 'overview';
		$data["query_car"] = $this->car_model->getCar();

		$this->template->load("administrator/template/main_template",$page,$data);
	}


	function load_list()
	{
		$type = $this->uri->segment(4);
		$car_id = $this->uri->segment(5);
		
		$arr['data'] = array();
		$query = $this->car_model->getAll($car_id, $type);
		
		foreach($query->result() as $row)
		{
			$data = array();
			$created = "<span class='username-tbl-info'>" . $row->username . "</span>";
			$created .= "<br><span class='time-tbl-info'>" . $row->time_created . "</span>";
			$link = "<a href='#' class='btn yellow edit-button' title='Edit Data' onclick='editData(" . $row->id . ")'><i class='fa fa-pencil'></i></a>";
			$link .= "<a href='#' class='btn red delete-button' title='Delete Data' onclick='deleteData(" . $row->id . ")'><i class='fa fa-minus-circle'></i></a>";
			array_push($data,$row->order_num);
			array_push($data,$created);
			array_push($data,$row->series_name);
			array_push($data,$row->title);
			array_push($data,$row->style);
			array_push($data,$link);
			
			array_push($arr['data'],$data);
		}		
		//echo $car_id;
		echo json_encode($arr);
	}

	function add()
	{
		if($_FILES['image']['size'] == 0)
		{
			//$data['image'] = $row->image;
			//echo 'kosong';
			die();
		}
		else
		{
			$config['upload_path'] = './uploads/car_overview';
			$config['allowed_types'] = 'gif|jpg|png';
			$config['max_size']  = '1000';
			$config['max_width']  = '3000';
			$config['max_height']  = '3000';

			$this->load->library('upload', $config);

			if ( ! $this->upload->do_upload('image')){
				$error = array('error' => $this->upload->display_errors());
				throw new Exception('Test error', 123);
			}
			else{
				
				$upload = $this->upload->data();

				$data["id"] = "";
				$data["users_id"] = $this->session->userdata("user_id");
				$data["time_created"] = gmdate('Y-m-d H:i:s', time()+60*60*7);
				$data["title"] = $this->input->post("title");
				$data["content1"] = $this->input->post("editor1");
				$data["content2"] = $this->input->post("editor2");
				$data["style"] = $this->input->post("style");
				$data["order_num"] = $this->input->post("order_num");
				$data["car_id"] = $this->input->post("car_id");
				$data["type"] = $this->input->post("type");
				$data["image"] = $upload['file_name'];

				$this->crud_model->save('car_overview',$data);
				echo $data["content1"];
			}
		}

		
	}

	function edit()
	{
		$id = $this->uri->segment(4);
		
		$row = $this->car_model->getOverviewByID($id)->row();
		
		$arr = array(array('id' => $id, 
						   'title' => $row->title, 
						   'content1' => $row->content1, 
						   'content2' => $row->content2, 
						   'order_num' => $row->order_num,
						   'image' => $row->image,
						   'style' => $row->style
						   )
					);
		echo json_encode($arr );
		
	}

	function update()
	{
		
		$id = $this->input->post('id');
		$row = $this->car_model->getOverviewByID($id)->row();

		if($_FILES['image']['size'] == 0)
		{
			
			$data["users_id"] = $this->session->userdata("user_id");
			$data["time_created"] = gmdate('Y-m-d H:i:s', time()+60*60*7);
			$data["title"] = $this->input->post("title");
			$data["content1"] = $this->input->post("editor1");
			$data["content2"] = $this->input->post("editor2");
			$data["style"] = $this->input->post("style");
			$data["order_num"] = $this->input->post("order_num");

			$this->crud_model->update('car_overview', $id, $data, 'id');
		}
		else
		{

			$config['upload_path'] = './uploads/car_overview';
			$config['allowed_types'] = 'gif|jpg|png';
			$config['max_size']  = '1000';
			$config['max_width']  = '3000';
			$config['max_height']  = '3000';

			$this->load->library('upload', $config);

			if ( ! $this->upload->do_upload('image')){
				$error = array('error' => $this->upload->display_errors());
				throw new Exception('Test error', 123);
			}
			else{
				unlink('./uploads/car_overview/'.$row->image);

				$upload = $this->upload->data();

				$data["users_id"] = $this->session->userdata("user_id");
				$data["time_created"] = gmdate('Y-m-d H:i:s', time()+60*60*7);
				$data["title"] = $this->input->post("title");
				$data["content1"] = $this->input->post("editor1");
				$data["content2"] = $this->input->post("editor2");
				$data["style"] = $this->input->post("style");
				$data["order_num"] = $this->input->post("order_num");
				$data["image"] = $upload['file_name'];

				$this->crud_model->update('car_overview', $id, $data, 'id');
			}
		}		
	}

	function delete()
	{
		$id = $this->input->post('id');
		$row = $this->car_model->getOverviewByID($id)->row();

		$this->crud_model->delete('car_overview', $id, 'id');

		unlink('./uploads/car_overview/'.$row->image);
		return true;
	}


	//
	////	THIS SECTION FOR DESCRIPTION
	function description()
	{
		$page["_contents"] = "administrator/car/v_car";
		$data['type'] = 'description';
		$data["query_car"] = $this->car_model->getCar();

		$this->template->load("administrator/template/main_template",$page,$data);
	}

	//
	////	THIS SECTION FOR SPECIFICATION
	function specification()
	{
		$page["_contents"] = "administrator/car/v_car";
		$data['type'] = 'specification';
		$data["query_car"] = $this->car_model->getCar();

		$this->template->load("administrator/template/main_template",$page,$data);
	}

	//
	////	THIS SECTION FOR FEATURE
	function feature()
	{
		$page["_contents"] = "administrator/car/v_car";
		$data['type'] = 'feature';
		$data["query_car"] = $this->car_model->getCar();

		$this->template->load("administrator/template/main_template",$page,$data);
	}

	//
	////	THIS SECTION FOR TYPE
	function type()
	{
		$page["_contents"] = "administrator/car/v_car_type";
		$this->template->load("administrator/template/main_template",$page);
	}

	function load_car_list()
	{
		$arr['data'] = array();
		$query = $this->car_model->getAllCar();
		
		foreach($query->result() as $row)
		{
			$data = array();
			$link = "<a href='#' class='btn yellow edit-button' title='Edit Data' onclick='editData(" . $row->car_id . ")'><i class='fa fa-pencil'></i></a>";
			$link .= "<a href='#' class='btn red delete-button' title='Delete Data' onclick='deleteData(" . $row->car_id . ")'><i class='fa fa-minus-circle'></i></a>";
			array_push($data,$row->series_name);
			array_push($data,$link);
			
			array_push($arr['data'],$data);
		}		
		//echo $car_id;
		echo json_encode($arr);
	}

	function add_type()
	{
		if($_FILES['image']['size'] == 0)
		{
			$data["car_id"] = "";
			$data["series_name"] = $this->input->post("series_name");

		}
		else
		{
			$config['upload_path'] = './uploads/car_type';
			$config['allowed_types'] = 'gif|jpg|png';
			$config['max_size']  = '1000';
			$config['max_width']  = '280';
			$config['max_height']  = '160';

			$this->load->library('upload', $config);

			if ( ! $this->upload->do_upload('image')){
				$error = array('error' => $this->upload->display_errors());
				throw new Exception('Test error', 123);
			}
			else{
				
				$upload = $this->upload->data();

				$data["car_id"] = "";
				$data["series_name"] = $this->input->post("series_name");

				$data["image"] = $upload['file_name'];
			}
		}

		$this->crud_model->save('car',$data);
		return true;

	}

	function edit_type()
	{
		$id = $this->uri->segment(4);
		
		$row = $this->car_model->getCarByID($id)->row();
		
		$arr = array(array('id' => $id, 
						   'series_name' => $row->series_name,
						   'image' => $row->image
						   )
					);
		echo json_encode($arr );
		
	}	

	function update_type()
	{
		$id = $this->input->post("car_id");
		$row = $this->car_model->getCarByID($id)->row();

		if($_FILES['image']['size'] == 0)
		{
			
			$data["series_name"] = $this->input->post("series_name");

		}
		else
		{

			$config['upload_path'] = './uploads/car_type';
			$config['allowed_types'] = 'gif|jpg|png';
			$config['max_size']  = '1000';
			$config['max_width']  = '280';
			$config['max_height']  = '160';

			$this->load->library('upload', $config);

			if ( ! $this->upload->do_upload('image')){
				$error = array('error' => $this->upload->display_errors());
				throw new Exception('Test error', 123);
			}
			else{
				unlink('./uploads/car_type/'.$row->image);

				$upload = $this->upload->data();

				$data["series_name"] = $this->input->post("series_name");
				$data["image"] = $upload['file_name'];

			}
		}		

		
		
		$this->crud_model->update('car', $id, $data, 'car_id');
		return true;
	}
	function delete_type()
	{
		$id = $this->input->post('id');
		$row = $this->car_model->getCarByID($id)->row();

		$this->crud_model->delete('car', $id, 'car_id');

		unlink('./uploads/car_type/'.$row->image);
		return true;
	}

	//
	////	THIS SECTION FOR GALLERY
	function gallery()
	{
		$page["_contents"] = "administrator/car/v_car_gallery";
		$data["query_car"] = $this->car_model->getCar();

		$this->template->load("administrator/template/main_template",$page,$data);
	}

	function load_gallery_list()
	{
		$car_id = $this->uri->segment(4);
		$arr['data'] = array();
		$query = $this->car_model->getGalleryByCar($car_id);
		
		foreach($query->result() as $row)
		{
			$data = array();
			if($row->cover_image == '0')
			{
				$cover = '';
				$link = "<a href='#' class='btn green delete-button' title='Make Cover Image' onclick='coverImage(" . $row->id . ")'><i class='fa fa-check'></i></a>";
				$link .= "<a href='#' class='btn red delete-button' title='Delete Data' onclick='deleteData(" . $row->id . ")'><i class='fa fa-minus-circle'></i></a>";
			}
			else
			{
				$cover = '<span class="label label-sm label-success">Cover Image</span>';
				$link = "<a href='#' class='btn red delete-button' title='Delete Data' onclick='deleteData(" . $row->id . ")'><i class='fa fa-minus-circle'></i></a>";	
			}

			$image = "<img width='100px' src='". base_url() ."uploads/car_gallery/". $row->image ."' /> ";
			array_push($data,$image);
			array_push($data,$cover);
			array_push($data,$link);
			
			array_push($arr['data'],$data);
		}		
		//echo $car_id;
		echo json_encode($arr);
	}


	function upload_gallery()
	{
		$config['upload_path'] = './uploads/car_gallery';
		$config['allowed_types'] = 'gif|jpg|png';
		$config['max_size']  = '1000';
		$config['max_width']  = '3000';
		$config['max_height']  = '3000';

		$this->load->library('upload', $config);

		if ( ! $this->upload->do_upload('file')){
			$error = array('error' => $this->upload->display_errors());
			throw new Exception('Test error', 123);
		}
		else{
			$upload = $this->upload->data();

			$data['car_id'] = $this->input->post('car_id');
			$data["image"] = $upload['file_name'];

			$this->crud_model->save('car_gallery',$data);
		}
	}

	function delete_gallery()
	{
		$id = $this->input->post('id');

		$this->crud_model->delete('car_gallery', $id, 'id');
	}


	function update_cover()
	{
		$id = $this->input->post('id');
		$data['cover_image'] = '1';

		$this->car_model->updateCover();
		$this->crud_model->update('car_gallery', $id, $data, 'id');
	}

	//
	////	THIS SECTION FOR SPAREPARTS
	function spareparts()
	{
		$page["_contents"] = "administrator/car/v_car_spareparts";
		$data["query_car"] = $this->car_model->getCar();

		$this->template->load("administrator/template/main_template",$page,$data);
	}

	function load_spareparts_list()
	{
		$car_id = $this->uri->segment(4);
		$arr['data'] = array();
		$query = $this->car_model->getAllCarSpareparts($car_id);
		
		foreach($query->result() as $row)
		{
			$data = array();
			$link = "<a href='#' class='btn yellow edit-button' title='Edit Data' onclick='editData(" . $row->id . ")'><i class='fa fa-pencil'></i></a>";
			$link .= "<a href='#' class='btn red delete-button' title='Delete Data' onclick='deleteData(" . $row->id . ")'><i class='fa fa-minus-circle'></i></a>";
			$pdf = "<a href='". base_url() ."uploads/car_spareparts/". $row->pdf ."' target='_blank'>PDF File</a>";
			array_push($data,$row->series_name);
			array_push($data,$pdf);
			array_push($data,$link);
			
			array_push($arr['data'],$data);
		}		
		//echo $car_id;
		echo json_encode($arr);
	}

	function add_spareparts()
	{
		if($_FILES['pdf']['size'] == 0)
		{
			$data["id"] = "";
			$data["car_id"] = $this->input->post('car_id');
			$data["description"] = $this->input->post('description');

			$this->crud_model->save('car_spareparts',$data);
		}
		else
		{
			$config['upload_path'] = './uploads/car_spareparts';
			$config['allowed_types'] = 'pdf';
			$config['max_size']  = '5000';

			$this->load->library('upload', $config);

			if ( ! $this->upload->do_upload('pdf')){
				$error = array('error' => $this->upload->display_errors());
				throw new Exception('Test error', 123);
			}
			else{
				
				$upload = $this->upload->data();

				$data["id"] = "";
				$data["car_id"] = $this->input->post('car_id');
				$data["description"] = $this->input->post('description');
				$data["pdf"] = $upload['file_name'];

				$this->crud_model->save('car_spareparts',$data);
			}
		}

		
	}

	function edit_spareparts()
	{
		$id = $this->uri->segment(4);
		
		$row = $this->car_model->getCarSparepartsByID($id)->row();
		
		$arr = array(array('id' => $id, 
						   'car_id' => $row->car_id,
						   'description' => $row->description,
						   'pdf' => $row->pdf
						   )
					);
		echo json_encode($arr );
		
	}

	function update_spareparts()
	{
		$id = $this->input->post('id');
		$row = $this->car_model->getCarSparepartsByID($id)->row();

		if($_FILES['pdf']['size'] == 0)
		{
			$data["car_id"] = $this->input->post("car_id");
			$data["description"] = $this->input->post("description");

			$this->crud_model->update('car_spareparts', $id, $data, 'id');
		}
		else
		{

			$config['upload_path'] = './uploads/car_spareparts';
			$config['allowed_types'] = 'pdf';
			$config['max_size']  = '5000';

			$this->load->library('upload', $config);

			if ( ! $this->upload->do_upload('pdf')){
				$error = array('error' => $this->upload->display_errors());
				throw new Exception('Test error', 123);
			}
			else{
				unlink('./uploads/car_spareparts/'.$row->pdf);

				$upload = $this->upload->data();

				$data["car_id"] = $this->input->post("car_id");
				$data["description"] = $this->input->post("description");
				$data["pdf"] = $upload['file_name'];

				$this->crud_model->update('car_spareparts', $id, $data, 'id');
			}
		}		
	}

	function delete_spareparts()
	{
		$id = $this->input->post('id');
		$row = $this->car_model->getCarSparepartsByID($id)->row();

		$this->crud_model->delete('car_spareparts', $id, 'id');
		unlink('./uploads/car_spareparts/'.$row->pdf);
	}



	//
	////	THIS SECTION FOR ACCESSORIES
	function accessories()
	{
		$page["_contents"] = "administrator/car/v_car_accessories";
		$data["query_car"] = $this->car_model->getCar();

		$this->template->load("administrator/template/main_template",$page,$data);
	}

	function load_accessories_list()
	{
		$car_id = $this->uri->segment(4);
		$arr['data'] = array();
		$query = $this->car_model->getAllCarAccessories($car_id);
		
		foreach($query->result() as $row)
		{
			$data = array();
			$link = "<a href='#' class='btn yellow edit-button' title='Edit Data' onclick='editData(" . $row->id . ")'><i class='fa fa-pencil'></i></a>";
			$link .= "<a href='#' class='btn red delete-button' title='Delete Data' onclick='deleteData(" . $row->id . ")'><i class='fa fa-minus-circle'></i></a>";
			$image = "<img src='". base_url() ."uploads/car_accessories/". $row->image ."' width='100px'>";
			array_push($data,$row->series_name);
			array_push($data,$row->name);
			array_push($data,$image);
			array_push($data,$link);
			
			array_push($arr['data'],$data);
		}		
		//echo $car_id;
		echo json_encode($arr);
	}

	function add_accessories()
	{
		if($_FILES['image']['size'] == 0)
		{
			$data["id"] = "";
			$data["car_id"] = $this->input->post('car_id');
			$data["description"] = $this->input->post('description');
			$data["name"] = $this->input->post("name");
			$this->crud_model->save('car_accessories',$data);
		}
		else
		{
			$config['upload_path'] = './uploads/car_accessories';
			$config['allowed_types'] = 'jpg|png|gif';
			$config['max_size']  = '3000';
			$config['max_size']  = '1000';
			$config['max_size']  = '1000';

			$this->load->library('upload', $config);

			if ( ! $this->upload->do_upload('image')){
				$error = array('error' => $this->upload->display_errors());
				throw new Exception('Test error', 123);
			}
			else{
				
				$upload = $this->upload->data();

				$data["id"] = "";
				$data["car_id"] = $this->input->post('car_id');
				$data["description"] = $this->input->post('description');
				$data["image"] = $upload['file_name'];
				$data["name"] = $this->input->post("name");
				$this->crud_model->save('car_accessories',$data);
			}
		}

		
	}

	function edit_accessories()
	{
		$id = $this->uri->segment(4);
		
		$row = $this->car_model->getCarAccessoriesByID($id)->row();
		
		$arr = array(array('id' => $id, 
						   'car_id' => $row->car_id,
						   'name' => $row->name,
						   'description' => $row->description,
						   'image' => $row->image
						   )
					);
		echo json_encode($arr );
		
	}

	function update_accessories()
	{
		$id = $this->input->post('id');
		$row = $this->car_model->getCarAccessoriesByID($id)->row();

		if($_FILES['image']['size'] == 0)
		{
			$data["car_id"] = $this->input->post("car_id");
			$data["description"] = $this->input->post("description");
			$data["name"] = $this->input->post("name");

			$this->crud_model->update('car_accessories', $id, $data, 'id');
		}
		else
		{

			$config['upload_path'] = './uploads/car_accessories';
			$config['allowed_types'] = 'jpg|png|gif';
			$config['max_size']  = '3000';
			$config['max_size']  = '1000';
			$config['max_size']  = '1000';

			$this->load->library('upload', $config);

			if ( ! $this->upload->do_upload('image')){
				$error = array('error' => $this->upload->display_errors());
				throw new Exception('Test error', 123);
			}
			else{
				unlink('./uploads/car_accessories/'.$row->image);

				$upload = $this->upload->data();

				$data["car_id"] = $this->input->post("car_id");
				$data["description"] = $this->input->post("description");
				$data["image"] = $upload['file_name'];
				$data["name"] = $this->input->post("name");

				$this->crud_model->update('car_accessories', $id, $data, 'id');
			}
		}		
	}


	function delete_accessories()
	{
		$id = $this->input->post('id');
		$row = $this->car_model->getCarAccessoriesByID($id)->row();

		$this->crud_model->delete('car_accessories', $id, 'id');
		unlink('./uploads/car_accessories/'.$row->image);
	}


	//
	////	THIS SECTION FOR BROCHURE
	function brochure()
	{
		$page["_contents"] = "administrator/car/v_car_brochure";
		$data["query_car"] = $this->car_model->getCar();

		$this->template->load("administrator/template/main_template",$page,$data);
	}

	function load_brochure_list()
	{
		$car_id = $this->uri->segment(4);
		$arr['data'] = array();
		$query = $this->car_model->getAllCarBrochure($car_id);
		
		foreach($query->result() as $row)
		{
			$data = array();
			$link = "<a href='#' class='btn yellow edit-button' title='Edit Data' onclick='editData(" . $row->id . ")'><i class='fa fa-pencil'></i></a>";
			$link .= "<a href='#' class='btn red delete-button' title='Delete Data' onclick='deleteData(" . $row->id . ")'><i class='fa fa-minus-circle'></i></a>";
			$pdf = "<a href='". base_url() ."uploads/car_brochure/". $row->pdf ."' target='_blank'>PDF File</a>";
			array_push($data,$row->series_name);
			array_push($data,$pdf);
			array_push($data,$link);
			
			array_push($arr['data'],$data);
		}		
		//echo $car_id;
		echo json_encode($arr);
	}

	function add_brochure()
	{
		if($_FILES['pdf']['size'] == 0)
		{
			$data["id"] = "";
			$data["car_id"] = $this->input->post('car_id');
			$data["description"] = $this->input->post('description');

			$this->crud_model->save('car_brochure',$data);
		}
		else
		{
			$config['upload_path'] = './uploads/car_brochure';
			$config['allowed_types'] = 'pdf';
			$config['max_size']  = '5000';

			$this->load->library('upload', $config);

			if ( ! $this->upload->do_upload('pdf')){
				$error = array('error' => $this->upload->display_errors());
				throw new Exception('Test error', 123);
			}
			else{
				
				$upload = $this->upload->data();

				$data["id"] = "";
				$data["car_id"] = $this->input->post('car_id');
				$data["description"] = $this->input->post('description');
				$data["pdf"] = $upload['file_name'];

				$this->crud_model->save('car_brochure',$data);
			}
		}

		
	}

		function edit_brochure()
	{
		$id = $this->uri->segment(4);
		
		$row = $this->car_model->getCarBrochureByID($id)->row();
		
		$arr = array(array('id' => $id, 
						   'car_id' => $row->car_id,
						   'description' => $row->description,
						   'pdf' => $row->pdf
						   )
					);
		echo json_encode($arr );
		
	}


	function update_brochure()
	{
		$id = $this->input->post('id');
		$row = $this->car_model->getCarBrochureByID($id)->row();

		if($_FILES['pdf']['size'] == 0)
		{
			$data["car_id"] = $this->input->post("car_id");
			$data["description"] = $this->input->post("description");

			$this->crud_model->update('car_brochure', $id, $data, 'id');
		}
		else
		{

			$config['upload_path'] = './uploads/car_brochure';
			$config['allowed_types'] = 'pdf';
			$config['max_size']  = '5000';

			$this->load->library('upload', $config);

			if ( ! $this->upload->do_upload('pdf')){
				$error = array('error' => $this->upload->display_errors());
				throw new Exception('Test error', 123);
			}
			else{
				unlink('./uploads/car_brochure/'.$row->pdf);

				$upload = $this->upload->data();

				$data["car_id"] = $this->input->post("car_id");
				$data["description"] = $this->input->post("description");
				$data["pdf"] = $upload['file_name'];

				$this->crud_model->update('car_brochure', $id, $data, 'id');
			}
		}		
	}

	function delete_brochure()
	{
		$id = $this->input->post('id');
		$row = $this->car_model->getCarBrochureByID($id)->row();

		$this->crud_model->delete('car_brochure', $id, 'id');
		unlink('./uploads/car_brochure/'.$row->pdf);
	}
	//
	////	THIS SECTION FOR CUSTOM
	function custom()
	{
		$data["query_car"] = $this->car_model->getCar();

		$page["_contents"] = "administrator/car/v_custom";
		$this->template->load("administrator/template/main_template",$page,$data);
	}


	function load_custom_list()
	{
		$arr['data'] = array();
		$query = $this->car_model->getAllCustom();
		
		foreach($query->result() as $row)
		{
			$data = array();
			$link = "<a href='#' class='btn yellow edit-button' title='Edit Data' onclick='editData(" . $row->id . ")'><i class='fa fa-pencil'></i></a>";
			$link .= "<a href='#' class='btn red delete-button' title='Delete Data' onclick='deleteData(" . $row->id . ")'><i class='fa fa-minus-circle'></i></a>";
			$created = "<span class='username-tbl-info'>" . $row->username . "</span>";
			$created .= "<br><span class='time-tbl-info'>" . $row->time_created . "</span>";
			if($row->status == '1')
			{
				$status = '<span class="label label-sm label-success">Published</span>';
			}
			else
			{
				$status = '<span class="label label-sm label-warning">Unpublished</span>';
			}
			array_push($data,$row->series_name);
			array_push($data,$created);
			array_push($data,$status);
			array_push($data,$link);
			
			array_push($arr['data'],$data);
		}		
		echo json_encode($arr);
	}

	function add_custom()
	{
		$data["id"] = "";
		$data["car_id"] = $this->input->post('car_id');
		$data["users_id"] = $this->session->userdata("user_id");
		$data["time_created"] = gmdate('Y-m-d H:i:s', time()+60*60*7);
		$data["content"] = $this->input->post("content");
		$data["status"] = $this->input->post("status");
		
		$this->crud_model->save('car_custom',$data);
		return true;
	}

	function edit_custom()
	{
		$id = $this->uri->segment(4);
		
		$row = $this->car_model->getCustomByID($id)->row();
		
		$arr = array(array('id' => $id, 
						   'car_id' => $row->car_id,
						   'content' => $row->content, 
						   'status' => $row->status
						   )
					);
		echo json_encode($arr );
		
	}

	function delete_custom()
	{
		$id = $this->input->post('id');
		
		$this->crud_model->delete('car_custom', $id, 'id');
		return true;
	}

	function update_custom()
	{
		$id = $this->input->post("id");;
		$data["users_id"] = $this->session->userdata('user_id');
		$data["time_created"] = gmdate('Y-m-d H:i:s', time()+60*60*7);
		$data["car_id"] = $this->input->post("car_id");
		$data["content"] = $this->input->post("content");
		$data["status"] = $this->input->post("status");
		
		$this->crud_model->update('car_custom', $id, $data, 'id');
		return true;
	}


	function load_list_publish()
	{
		$arr['data'] = array();
		$query = $this->car_model->getOverviewPublish();
		
		foreach($query->result() as $row)
		{
			$data = array();

			if($row->overview_status == '1')
			{
				$status = '<span class="label label-sm label-success">Published</span>';
				$link = "";
			}	
			else
			{
				$status = '<span class="label label-sm label-warning">Unpublished</span>';
				$link = "<a href='#' class='btn blue edit-button' title='Publish Overview' onclick='publish(" . $row->car_id . ")'><i class='fa fa-upload'></i></a>";
			}
			array_push($data,$row->series_name);
			array_push($data,$status);
			array_push($data,$link);
			array_push($arr['data'],$data);
		}		

		echo json_encode($arr);
	}
	

	function publish_overview()
	{
		$id = $this->input->post('car_id');
		$data['overview_status'] = '1';

		$this->car_model->updateOverview();
		$this->crud_model->update('car', $id, $data, 'car_id');
	}
}

/* End of file Car.php */
/* Location: ./application/controllers/administrator/Car.php */